{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "0a8550bf-735d-4866-95bd-3532030d1ed4",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "\n",
    "# df = pd.read_csv('603444SH.csv')\n",
    "# print(df.head(25))\n",
    "# df.iloc[0:16, :].T\n",
    "# for i in range(0, len(df), 18):\n",
    "    # print(df.iloc[i:i + 18, :].T.iloc[1:2, :].to_csv('demo.csv', mode='a', index=False, header=False))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "e700de4f-20af-46d1-a705-d7c8e3fb724c",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_csv('603444.csv')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "820252a3-39eb-4b18-939f-e483a4fa175b",
   "metadata": {},
   "source": [
    "# 海龟"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "338c5338-8ace-4c75-8d2f-0c0db2cb38e2",
   "metadata": {},
   "source": [
    "## https://www.myquant.cn/docs/python_strategyies/110"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "8a7e855c-bf74-4309-a260-1fab4b957c7d",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "9201782a-b4d4-4d69-b563-eae5b693b405",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "         date    open    high     low   close  volume\n",
      "0  2017-01-04   77.76   77.76   77.76   77.76      88\n",
      "1  2017-01-05   85.54   85.54   85.54   85.54      44\n",
      "2  2017-01-06   94.09   94.09   94.09   94.09      40\n",
      "3  2017-01-09  103.50  103.50  103.50  103.50     103\n",
      "4  2017-01-10  113.85  113.85  113.85  113.85     166\n"
     ]
    }
   ],
   "source": [
    "print(df.head())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "eae200a1-6b90-4234-a1b8-9ddd5622ca4b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0       2017-01-04\n",
       "1       2017-01-05\n",
       "2       2017-01-06\n",
       "3       2017-01-09\n",
       "4       2017-01-10\n",
       "           ...    \n",
       "1715    2024-01-23\n",
       "1716    2024-01-24\n",
       "1717    2024-01-25\n",
       "1718    2024-01-26\n",
       "1719    2024-01-29\n",
       "Name: date, Length: 1720, dtype: object"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.iloc[:,0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "79d37675-b3d8-4875-aa37-a13340dc369d",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "           date    open    high     low   close  volume\n",
      "0    2017-01-04   77.76   77.76   77.76   77.76      88\n",
      "1    2017-01-05   85.54   85.54   85.54   85.54      44\n",
      "2    2017-01-06   94.09   94.09   94.09   94.09      40\n",
      "3    2017-01-09  103.50  103.50  103.50  103.50     103\n",
      "4    2017-01-10  113.85  113.85  113.85  113.85     166\n",
      "...         ...     ...     ...     ...     ...     ...\n",
      "1715 2024-01-23  196.01  207.87  196.01  204.96   24196\n",
      "1716 2024-01-24  206.00  207.00  195.57  201.63   16086\n",
      "1717 2024-01-25  201.81  206.60  200.08  205.60   20580\n",
      "1718 2024-01-26  208.06  211.31  203.00  203.28   21418\n",
      "1719 2024-01-29  203.81  204.50  192.68  194.83   18876\n",
      "\n",
      "[1720 rows x 6 columns]\n"
     ]
    }
   ],
   "source": [
    "df['date'] = pd.to_datetime(df['date'], format='%Y-%m-%d')\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 157,
   "id": "a246e84f-c1be-4d69-a4f2-13190597b5af",
   "metadata": {},
   "outputs": [],
   "source": [
    "test_df =  df[df['date'].between('2020-01-01', '2024-02-01')]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 158,
   "id": "ffab9b18-35b0-4009-ad32-fddd81dcb7c0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>date</th>\n",
       "      <th>open</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>close</th>\n",
       "      <th>volume</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>730</th>\n",
       "      <td>2020-01-02</td>\n",
       "      <td>301.68</td>\n",
       "      <td>315.61</td>\n",
       "      <td>296.11</td>\n",
       "      <td>313.34</td>\n",
       "      <td>16772</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>731</th>\n",
       "      <td>2020-01-03</td>\n",
       "      <td>311.95</td>\n",
       "      <td>315.00</td>\n",
       "      <td>305.38</td>\n",
       "      <td>306.39</td>\n",
       "      <td>8659</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>732</th>\n",
       "      <td>2020-01-06</td>\n",
       "      <td>306.31</td>\n",
       "      <td>337.03</td>\n",
       "      <td>306.31</td>\n",
       "      <td>337.03</td>\n",
       "      <td>27138</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>733</th>\n",
       "      <td>2020-01-07</td>\n",
       "      <td>337.04</td>\n",
       "      <td>365.00</td>\n",
       "      <td>331.18</td>\n",
       "      <td>363.55</td>\n",
       "      <td>22965</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>734</th>\n",
       "      <td>2020-01-08</td>\n",
       "      <td>356.41</td>\n",
       "      <td>366.36</td>\n",
       "      <td>355.00</td>\n",
       "      <td>357.02</td>\n",
       "      <td>12585</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1715</th>\n",
       "      <td>2024-01-23</td>\n",
       "      <td>196.01</td>\n",
       "      <td>207.87</td>\n",
       "      <td>196.01</td>\n",
       "      <td>204.96</td>\n",
       "      <td>24196</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1716</th>\n",
       "      <td>2024-01-24</td>\n",
       "      <td>206.00</td>\n",
       "      <td>207.00</td>\n",
       "      <td>195.57</td>\n",
       "      <td>201.63</td>\n",
       "      <td>16086</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1717</th>\n",
       "      <td>2024-01-25</td>\n",
       "      <td>201.81</td>\n",
       "      <td>206.60</td>\n",
       "      <td>200.08</td>\n",
       "      <td>205.60</td>\n",
       "      <td>20580</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1718</th>\n",
       "      <td>2024-01-26</td>\n",
       "      <td>208.06</td>\n",
       "      <td>211.31</td>\n",
       "      <td>203.00</td>\n",
       "      <td>203.28</td>\n",
       "      <td>21418</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1719</th>\n",
       "      <td>2024-01-29</td>\n",
       "      <td>203.81</td>\n",
       "      <td>204.50</td>\n",
       "      <td>192.68</td>\n",
       "      <td>194.83</td>\n",
       "      <td>18876</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>990 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "           date    open    high     low   close  volume\n",
       "730  2020-01-02  301.68  315.61  296.11  313.34   16772\n",
       "731  2020-01-03  311.95  315.00  305.38  306.39    8659\n",
       "732  2020-01-06  306.31  337.03  306.31  337.03   27138\n",
       "733  2020-01-07  337.04  365.00  331.18  363.55   22965\n",
       "734  2020-01-08  356.41  366.36  355.00  357.02   12585\n",
       "...         ...     ...     ...     ...     ...     ...\n",
       "1715 2024-01-23  196.01  207.87  196.01  204.96   24196\n",
       "1716 2024-01-24  206.00  207.00  195.57  201.63   16086\n",
       "1717 2024-01-25  201.81  206.60  200.08  205.60   20580\n",
       "1718 2024-01-26  208.06  211.31  203.00  203.28   21418\n",
       "1719 2024-01-29  203.81  204.50  192.68  194.83   18876\n",
       "\n",
       "[990 rows x 6 columns]"
      ]
     },
     "execution_count": 158,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 159,
   "id": "04dcc07e-5c00-4f15-9817-8c0b27e858ca",
   "metadata": {},
   "outputs": [],
   "source": [
    "high_price_trade_date = 5\n",
    "low_price_trade_date = 3\n",
    "capital = 150000\n",
    "hold = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 160,
   "id": "83c9d567-52c3-4085-b972-0b9360899d9b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "买了4手,余额:15184.0,花费:134816.00\n",
      "卖了4手,余额:149608.0, 金额:134424.0\n",
      "买了3手,余额:24979.0,花费:124629.00\n",
      "卖了3手,余额:192937.0, 金额:167958.0\n",
      "买了3手,余额:9043.0,花费:183894.00\n",
      "卖了3手,余额:192634.0, 金额:183591.0\n",
      "买了3手,余额:5134.0,花费:187500.00\n",
      "卖了3手,余额:160855.00000000003, 金额:155721.00000000003\n",
      "买了3手,余额:35755.00000000003,花费:125100.00\n",
      "卖了3手,余额:146785.00000000003, 金额:111030.0\n",
      "买了4手,余额:385.0000000000291,花费:146400.00\n",
      "卖了4手,余额:214505.0, 金额:214119.99999999997\n",
      "买了6手,余额:3503.0,花费:211002.00\n",
      "卖了6手,余额:209219.0, 金额:205716.0\n",
      "买了5手,余额:31719.0,花费:177500.00\n",
      "卖了5手,余额:209944.0, 金额:178225.0\n",
      "买了7手,余额:8344.0,花费:201600.00\n",
      "卖了7手,余额:181804.0, 金额:173460.0\n",
      "买了7手,余额:3654.0,花费:178150.00\n",
      "卖了7手,余额:225701.0, 金额:222047.0\n",
      "买了6手,余额:5045.0,花费:220656.00\n",
      "卖了6手,余额:220205.0, 金额:215160.0\n",
      "买了5手,余额:20205.0,花费:200000.00\n",
      "卖了5手,余额:269740.0, 金额:249535.0\n",
      "买了5手,余额:20240.0,花费:249500.00\n",
      "卖了5手,余额:277690.0, 金额:257450.0\n"
     ]
    }
   ],
   "source": [
    "for index, row in test_df.iterrows():\n",
    "    max_price = max(df[index-high_price_trade_date:index]['high'])\n",
    "    min_price = min(df[index-low_price_trade_date:index]['low'])\n",
    "    open = row['open']\n",
    "    low = row['low']\n",
    "    last_price = open * 100 \n",
    "    if open > max_price and capital >= last_price:\n",
    "        can_buy = int(capital / (open*100))\n",
    "        capital = capital - can_buy*(open*100)\n",
    "        hold= hold+can_buy\n",
    "        print(f'买了{can_buy}手,余额:{capital},花费:{can_buy*(open*100):.2f}')\n",
    "    elif open < min_price and hold > 0:\n",
    "        sell_money = hold * 100 * low\n",
    "        capital = capital + sell_money\n",
    "        print(f'卖了{hold}手,余额:{capital}, 金额:{sell_money}')\n",
    "        hold = 0\n",
    "    # print(index, row['date_column'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 161,
   "id": "0f1ab5d7-bc29-4cf5-835e-fa35bcc90896",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "277690.0"
      ]
     },
     "execution_count": 161,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "capital"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "43570c46-5aad-4353-b66b-03c18461ea9f",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
